
#include "Tools.h"

// 安全编码转换函数
char* WideToUTF8(const wchar_t* wide_str)
{
	if (!wide_str)
		return NULL;

	int utf8_len = WideCharToMultiByte(CP_UTF8, 0, wide_str, -1, NULL, 0, NULL, NULL);
	if (utf8_len <= 0)
		return NULL;

	char* utf8_str = (char*)malloc(utf8_len);
	if (utf8_str)
		WideCharToMultiByte(CP_UTF8, 0, wide_str, -1,
		                    utf8_str, utf8_len, NULL, NULL);

	return utf8_str;
}
wchar_t* UTF8ToWide(const char* utf8_str, uint32_t utf8_len)
{
	if (!utf8_str || utf8_len == 0)
		return NULL;

	int wide_len = MultiByteToWideChar(CP_UTF8, 0, utf8_str, utf8_len, NULL, 0);
	if (wide_len <= 0)
		return NULL;

	wchar_t* wide_str = (wchar_t*)malloc((wide_len + 1) * sizeof(wchar_t));
	if (wide_str)
	{
		MultiByteToWideChar(CP_UTF8, 0, utf8_str, utf8_len, wide_str, wide_len);
		wide_str[wide_len] = L'\0';
	}

	return wide_str;
}
wchar_t* AgrvToWide(const char* utf8_str, uint32_t utf8_len)
{
	if (!utf8_str || utf8_len == 0)
		return NULL;

	int wide_len = MultiByteToWideChar(CP_UTF8, 0, utf8_str, utf8_len, NULL, 0);
	if (wide_len <= 0)
		return NULL;

	wchar_t* wide_str = (wchar_t*)malloc((wide_len + 1) * sizeof(wchar_t));
	if (!wide_str)
		return NULL;

	// 执行实际转换
	if (MultiByteToWideChar(CP_UTF8, 0, utf8_str, utf8_len, wide_str, wide_len) == 0)
	{
		free(wide_str);
		return NULL;
	}
	return wide_str;
}

//加密解密函数
void process_file_W(const wchar_t* filename_in, const wchar_t* filename_out, const char* password)
{
	FILE* file = _wfopen(filename_in, L"rb+");//“rb+”确保了该文件必须存在
	FILE* file_out = _wfopen(filename_out, L"wb+");

	if (!file)
	{
		perror("无法打开文件");
		exit(EXIT_FAILURE);
	}

	// 获取文件大小
	fseek(file, 0, SEEK_END);
	long file_size = ftell(file);
	fseek(file, 0, SEEK_SET);

	// 分配内存缓冲区
	unsigned char* buffer = (unsigned char*)malloc(file_size);
	if (!buffer)
	{
		perror("内存分配失败");
		flag_error(file);
	}

	// 读取文件内容
	size_t bytes_read = fread(buffer, 1, file_size, file);
	if (bytes_read != file_size)
	{
		perror("文件读取错误");
		free(buffer);
		flag_error(file);
	}

	// 处理加密/解密
	int pass_len = strlen(password);
	if (pass_len == 0)
	{
		fprintf(stderr,"错误：密码不能为空\n");
		free(buffer);
		flag_error(file);
	}
	for (long i = 0; i < file_size; i++)
		buffer[i] ^= password[i % pass_len];

	// 写回文件
	fseek(file, 0, SEEK_SET);
	size_t bytes_written = fwrite(buffer, 1, file_size, file_out);
	if (bytes_written != file_size)
		perror("文件写入错误");

	// 清理资源
	free(buffer);
	fclose(file), fclose(file_out);

	printf("操作成功完成！\n");
}
void process_file(const char* filename_in, const char* filename_out, const char* password)
{
	FILE* file = fopen(filename_in, "rb+");//“rb+”确保了该文件必须存在
	FILE* file_out = fopen(filename_out, "wb+");

	if (!file)
	{
		perror("无法打开文件");
		exit(EXIT_FAILURE);
	}

	// 获取文件大小
	fseek(file, 0, SEEK_END);
	long file_size = ftell(file);
	fseek(file, 0, SEEK_SET);

	// 分配内存缓冲区
	unsigned char* buffer = (unsigned char*)malloc(file_size);
	if (!buffer)
	{
		perror("内存分配失败");
		flag_error(file);
	}

	// 读取文件内容
	size_t bytes_read = fread(buffer, 1, file_size, file);
	if (bytes_read != file_size)
	{
		perror("文件读取错误");
		free(buffer);
		flag_error(file);
	}

	// 处理加密/解密
	int pass_len = strlen(password);
	if (pass_len == 0)
	{
		fprintf(stderr,"错误：密码不能为空\n");
		free(buffer);
		flag_error(file);
	}
	for (long i = 0; i < file_size; i++)
		buffer[i] ^= password[i % pass_len];

	// 写回文件
	// 无缓冲区格式
	fseek(file, 0, SEEK_SET);
	size_t bytes_written = fwrite(buffer, 1, file_size, file_out);
	if (bytes_written != file_size)
		perror("文件写入错误");
	/*
		//缓冲区格式
		char buffer_[BUFFER_SIZE];
		size_t bytes_written;

		fseek(file, 0, SEEK_SET);
		while ((bytes_written = fread(buffer_, 1, BUFFER_SIZE, file)) > 0)
			fwrite(buffer_, 1, bytes_written, file_out); //直接打包。
	*/

	// 清理资源
	free(buffer);
	fclose(file), fclose(file_out);

	printf("操作成功完成！\n");

}